<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增【精彩瞬间】')" />
    <th:block th:include="include :: header('文件上传')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-img-add" enctype="multipart/form-data">
            <input name="id" type="hidden">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <div class="form-group">
                            <label class="font-noraml">单图片文件上传</label>
                            <div class="file-loading">
                                <input id="file" type="file" multiple name="file">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script type="text/javascript">
        var prefix = ctx + "website/img"
        $("#form-img-add").validate({
            focusCleanup: true
        });
        $(document).ready(function () {
            $("#file").fileinput({
                'theme': 'explorer-fas',
                'uploadUrl': prefix+'/upload',
                overwriteInitial: false,
                initialPreviewAsData: true,
                allowedFileExtensions : ['jpg', 'png','gif'],//接收的文件后缀,
                showUpload: false,
                contentType: false
            });

            $("#file").fileinput({
                'theme': 'explorer-fas',
                'uploadUrl': '/website/img/upload',
                overwriteInitial: false,
                initialPreviewAsData: true,
                allowedFileExtensions : ['jpg', 'png','gif'],
                contentType: false,
                showUpload: false,
                uploadExtraData: function(previewId, index) {   //额外参数的关键点
                    var obj = {};
                    obj.id = fodderType();
                    console.log(obj);
                    return obj;
                }
            });


            // 上传附件
            // 上传成功回调
            $("#file").on("filebatchuploadcomplete", function() {
                //layer.msg("上传附件成功");
                $.modal.msgReload("上传附件成功",'success');
                //$.modal.close();
                //setTimeout("closeUpladLayer()",2000)
            });
            // 上传失败回调
            $('#file').on('fileerror', function(event, data, msg) {
                $.modal.msgReload("上传附件失敗", 'error');
                // alert(event+" | "+data+" | "+msg);
                //tokenTimeOut(data);
            });
        });

        function submitHandler() {
            // $.modal.close();
            // return true;
            var id=null;
            if ($.validate.form()) {
                //$.operate.save(prefix + "/website/img/upload?file="+$("#file")[0].files[0]+"id="+id, $('#form-img-add').serialize());
                    var formData = new FormData();
                    formData.append("file", $("#file")[0].files[0]);
                    //console.log($("#file")[0].files[0])
                    if ($("#file")[0].files[0] == null) {
                        $.modal.msgError("请选择图片上传！")
                    } else {
                        var name=$("#file")[0].files[0]['name'];
                        var suffix=name.substring(name.lastIndexOf('.'),name.length);
                        if(suffix == '.jpg' || suffix=='.png' || suffix=='.gif'){
                            $.ajax({
                                url: prefix + '/upload',
                                type: 'post',
                                data: formData,
                                contentType: false,
                                processData: false,
                                dataType: "json",
                                success: function (data) {
                                    if (data.code==0) {
                                        $.modal.msgReload(data.msg,'success');
                                    }else if(data.code==301){
                                        $.modal.msg(data.msg,'warn');
                                    } else {
                                        $.modal.msgReload(data.msg, 'error');
                                    }
                                }
                            })
                        }else{
                            $.modal.msgError("请选择图片上传！")
                        }
                    }
            }
        }
    </script>
</body>
</html>